package com.teira.rhrrealstate.session;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

@Name("authenticator")
public class Authenticator
{
	@In EntityManager entityManager;

	   @In Credentials credentials;

	   @In Identity identity;


	   public boolean authenticate() {

	      try {

	         entityManager.createQuery(
	            "from Usuario where username = :username and passwordhash = :password")
	            .setParameter("username", credentials.getUsername())
	            .setParameter("password", credentials.getPassword())
	            .getSingleResult();
	            identity.addRole("Admin");
	         return true;
	      }

	      catch (NoResultException ex) {
	         return false;
	      }
	   }
}